package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.BocomB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BocomConstants;
import com.gopay.common.constants.bank.BocomOrderStatus;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.BocomB2cMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.BocomB2cQueryDetail;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

public class BocomB2cBankServiceImpl implements BankInteractRemoteService {

	@Resource(name = "bocomB2cManager")
	private BocomB2cManager bocomB2cManager;

	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		try {
			BocomB2cMessage messageB2C = new BocomB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			messageB2C = bocomB2cManager.sign(messageB2C);
			base.setBankOrderId(base.getOrderId());
			result.setDefaultModel(messageB2C);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}

	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		try {
			// B2C验签
			BocomB2cMessage messageB2C = new BocomB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			boolean flag = (Boolean) bocomB2cManager.verify(messageB2C);
			result.setDefaultModel(flag);
			result.setSuccess(true);
			result.setModel("res", messageB2C);

			Map<String, String> param = new HashMap<String, String>();
			// 通知后台地址入账
			if (messageB2C.getRetCode().equals("1")) {										
				param.put("orderId", messageB2C.getOrderId());
				param.put("amt", messageB2C.getAmount().toString());
				param.put("cardType", "Unknown");
				param.put("notifyNum", "1");
				param.put("payStatus", "00");
				param.put("retCode", "00");
				param.put("retMsg", "验签成功");
				param.put("signData", messageB2C.getSignMessage());
				param.put("isFront", String.valueOf(base.isFront()));
				result.setModel("isNotifyCashier", param);
				result.setOrderStatus(true);
			} else {
				// 通知后台地址该订单失败
				param.put("orderId", messageB2C.getOrderId());
				param.put("amt", messageB2C.getAmount().toString());
				param.put("cardType", "Unknown");
				param.put("notifyNum", "1");
				param.put("payStatus", "01");
				param.put("retCode", "01");
				param.put("retMsg", messageB2C.getErrorMessage());
				param.put("signData", messageB2C.getSignMessage());
				param.put("isFront", String.valueOf(base.isFront()));
				result.setModel("isNotifyCashier", param);
				result.setOrderStatus(false);
			}
		
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
		return result;
	}

	@Override
	public Result query(BaseMessage base) {
		Result result = new Result(false);
		try {
			BocomB2cMessage messageB2C = new BocomB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			BocomB2cMessage res = bocomB2cManager.query(messageB2C);
			result.setDefaultModel(res);
			result.setSuccess(true);
			if(BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())){
				Map<String, String> param = new HashMap<String, String>();
				param.put("orderId", messageB2C.getOrderId());
				param.put("amount", messageB2C.getAmount().toString());
				if (res == null || res.getRetCode() == null) {
					// 查询订单为空
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行返回对账结果为空");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				// 通信状态码错误
				if (!res.getRetCode().equals(BocomConstants.B2C_RETCODE_SUCCESS)) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", res.getErrorMessage());
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				if (res.getQueryDetails() != null && res.getQueryDetails().size() != 0) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);	
					BocomB2cQueryDetail orderRes = res.getQueryDetails().get(0);
					//成功处理
					if (orderRes.getOrderState().equals(BocomConstants.B2C_ORDERSTATE_PAID) && BocomConstants.B2C_ORDERSTATE_SUCCESS.equals(orderRes.getTranState())) {					
						param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
						param.put("returnMessage", "交易成功");
					}
					//失败处理
					else if (orderRes.getOrderState().equals(BocomConstants.B2C_ORDERSTATE_PAID) && BocomConstants.B2C_ORDERSTATE_REVOCATION.equals(orderRes.getTranState())) {
						param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
						param.put("returnMessage", res.getErrorMessage());
					}
					//不处理
					else {
						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", BocomOrderStatus.getName(orderRes.getOrderState()));
					}
				}else{
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);			
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					param.put("returnMessage", "未查找到订单信息，订单查询返回码："+ res.getRetCode());
				}
				result.setModel("queryResult", param);
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
		return result;
	}

	@Override
	public Result refund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result dayend(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	public BocomB2cManager getBocomB2cManager() {
		return bocomB2cManager;
	}

	public void setBocomB2cManager(BocomB2cManager bocomB2cManager) {
		this.bocomB2cManager = bocomB2cManager;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}
}
